package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.markerhub.entity.Course;
import com.markerhub.entity.ManagerActivity;
import com.markerhub.entity.ManagerStudent;
import com.markerhub.mapper.CourseMapper;
import com.markerhub.mapper.ManagerActivityMapper;
import com.markerhub.mapper.ManagerStudentMapper;
import com.markerhub.service.AdminDashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AdminDashboardServiceImpl implements AdminDashboardService {

    @Autowired
    private ManagerActivityMapper activityMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private ManagerStudentMapper studentMapper;
    @Override
    public Map<String, Object> getStats() {
        Map<String, Object> stats = new HashMap<>();

        // 活动统计
        stats.put("activityCount", getActivityCount());
        stats.put("activeActivityCount", getActiveActivityCount());

        // 课程统计
        stats.put("courseCount", getCourseCount());
        stats.put("activeCourseCount", getActiveCourseCount());

        // 学生统计
        stats.put("studentCount", getStudentCount());
        stats.put("newStudentCount", getNewStudentCount());

        // 活动趋势数据
        stats.put("activityTrend", getActivityTrend());

        // 学生分布数据
        stats.put("studentDistribution", getStudentDistribution());

        return stats;
    }

    private Integer getActivityCount() {
        return activityMapper.selectCount(null);
    }

    private Integer getActiveActivityCount() {
        return activityMapper.selectCount(new LambdaQueryWrapper<ManagerActivity>()
                .eq(ManagerActivity::getStatus, 1));
    }

    private Integer getCourseCount() {
        return courseMapper.selectCount(null);
    }

    private Integer getActiveCourseCount() {
        return courseMapper.selectCount(new LambdaQueryWrapper<Course>()
                .eq(Course::getStatus, "1"));
    }

    private Integer getStudentCount() {
        return studentMapper.selectCount(null);
    }

    private Integer getNewStudentCount() {
        return studentMapper.selectCount(new LambdaQueryWrapper<ManagerStudent>()
                .eq(ManagerStudent::getIsFirst, "1"));
    }

    private List<Map<String, Object>> getActivityTrend() {
        return activityMapper.selectActivityTrend();
    }

    private List<Map<String, Object>> getStudentDistribution() {
        return studentMapper.selectStudentDistribution();
    }
}
